Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Revert named colors #345

Merged
merged 2 commits into from
Mar 11, 2022
Merged

Revert named colors #345

merged 2 commits into from
Mar 11, 2022

Conversation

eerhardt
Copy link
Member

The previous change caused a startup-regression with Color because it spends time building up a dictionary as soon as the Color type is touched (in its static ctor). Changing this code back to use pattern-matching, which forces us to allocate a string, for now. Once we have a compiler that supports pattern-matching on a ReadOnlySpan, we can do this allocation free.

This is similar to the original implementation, which also always allocated the lowered string to do pattern matching on.

Here are my benchmark results.

Orig without any of my Color changes

Method Mean Error StdDev Gen 0 Gen 1 Gen 2 Allocated
Parse 99.13 ns 0.281 ns 0.235 ns 0.0267 - - 168 B
ParseBlack 60.99 ns 0.907 ns 0.804 ns 0.0050 - - 32 B
ParseLightGoldenrodYellowWithSpace 88.82 ns 0.244 ns 0.216 ns 0.0204 - - 128 B

PR

Method Mean Error StdDev Gen 0 Gen 1 Gen 2 Allocated
Parse 52.54 ns 0.292 ns 0.259 ns 0.0051 - - 32 B
ParseBlack 69.61 ns 0.811 ns 0.759 ns 0.0050 - - 32 B
ParseLightGoldenrodYellowWithSpace 89.28 ns 0.710 ns 0.629 ns 0.0101 - - 64 B

So you can see ParseBlack is slightly slower than before. But when we get dotnet/csharplang#1881 it should be as fast, or faster, and allocation free.

The previous change caused a startup-regression with Color because it spends time building up a dictionary as soon as the Color type is touched (in its static ctor). Changing this code back to use pattern-matching, which forces us to allocate a string, for now. Once we have a compiler that supports pattern-matching on a ReadOnlySpan<char>, we can do this allocation free.

This is no worse than the original implementation, which also always allocated the lowered string to do pattern matching on.
@jonlipsky jonlipsky merged commit d4cb44a into dotnet:main Mar 11, 2022
@eerhardt eerhardt deleted the RevertNamedColors branch March 11, 2022 14:15
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants